<template>
  <div :class="prefixCls">
    <Popover v-model:open="popoverVisible" title="" trigger="click" :overlayClassName="`${prefixCls}__overlay`">
      <Badge :count="count" :overflowCount="99" :offset="[-4, 10]" :numberStyle="numberStyle">
        <BellOutlined />
      </Badge>
      <template #content>
        <Tabs @change="tabChange" :activeKey="tabKey">
          <template v-for="item in tabTypeList" :key="item.msgCategory">
            <TabPane>
              <template #tab>
                {{ item.msgCategoryName }}
                <span v-if="item.count !== 0">({{ item.count }})</span>
              </template>
              <!-- 绑定title-click事件的通知列表中标题是“可点击”的-->
            </TabPane>
          </template>
        </Tabs>

        <NoticeList :list="tabDataList" :msgCategory="msgCategory" :onTitleClick="onNoticeClick" v-model:currentPage="currentPage" />
        <a-row class="bottom-buttons">
          <a-col :span="count === 0 ? 0 : 12">
            <a-button @click="onEmptyNotify" type="dashed" block>清空消息</a-button>
          </a-col>
          <a-col :span="count === 0 ? 24 : 12">
            <a-button @click="popoverVisible = false" type="dashed" block>
              <router-link to="/system/message/mynews">查看更多</router-link>
            </a-button>
          </a-col>
        </a-row>
      </template>
    </Popover>
    <DynamicNotice ref="dynamicNoticeRef" v-bind="dynamicNoticeProps" />
    <DetailModal @register="registerDetail" />
  </div>
</template>
<script lang="ts">
import { computed, defineComponent, ref, unref, reactive, onMounted, getCurrentInstance, onUnmounted, watch } from 'vue';
import { Popover, Tabs, Badge } from 'ant-design-vue';
import { BellOutlined } from '@ant-design/icons-vue';
import { updateReadStatusMessage } from '/@/api/common/api';
import NoticeList from './NoticeList.vue';
import DetailModal from '/@/views/system/message/mynews/DetailModal.vue';
import DynamicNotice from '/@/views/system/message/mynews/DynamicNotice.vue';
import { useModal } from '/@/components/Modal';
import { useDesign } from '/@/hooks/web/useDesign';
import { useGlobSetting } from '/@/hooks/setting';
import { useUserStore } from '/@/store/modules/user';
import { connectWebSocket, onWebSocket, result } from '/@/hooks/web/useWebSocket';
import { updateAllUnreadMessage, getMessageGroupCount } from '/@/views/system/message/messageApi';
import { getToken } from '/@/common/util/auth';
import { useMessage } from '/@/hooks/web/useMessage'; // 轻提醒
const { createMessage: $message } = useMessage(); // 解构

export default defineComponent({
  components: {
    Popover,
    BellOutlined,
    Tabs,
    TabPane: Tabs.TabPane,
    Badge,
    NoticeList,
    DetailModal,
    DynamicNotice,
  },
  setup() {
    const { prefixCls } = useDesign('header-notify');
    const instance: any = getCurrentInstance();
    const userStore = useUserStore();
    const glob = useGlobSetting();
    const dynamicNoticeProps = reactive({ path: '', formData: {} });
    const [registerDetail, detailModal] = useModal();
    const popoverVisible = ref<boolean>(false);

    const tabTypeList = ref([
      {
        "msgCategory": "",
        "msgCategoryName": "",
        "list": null,
        "count": 0
      }
    ]);
    const tabDataList = ref([]);
    const currentPage = ref(1);
    const tabKey = ref('');
    const msgCategory = ref('');

    onMounted(() => {
      // initWebSocket();
      loadData();
    });

    watch(
      currentPage,
      (v) => {
        $message.info('测试1111......' + v);
      }
    );

    //计算总数
    const count = computed(() => {
      let count = 0;
      for (let i = 0; i < tabTypeList.value.length; i++) {
        count += tabTypeList.value[i].count;
      }
      return count;
    });

    // 获取系统消息
    async function loadData() {
      //获取总数量
      // count.value = await getMessageUnreadCount();
      let res = await getMessageGroupCount();
      tabTypeList.value = res;
      if (res.length > 0) {
        tabKey.value = res[0].msgCategory;
        //默认加载数据
        tabChange(res[0].msgCategory)
      }
    }

    // tab切换
    async function tabChange(key) {
      tabKey.value = key;
      //动态获取消息内容
      // let params = {
      //   msgCategory: key
      // }
      // let data = await getMessagePage(params);
      // tabDataList.value = data.records;
      msgCategory.value = key;
    }

    async function onNoticeClick(record) {
      $message.info('测试222......');
      try {
        await updateReadStatusMessage({ id: record.id });
        loadData();
      } catch (e) {
        console.error(e);
      }
      if (record.openType === 'component') {
        dynamicNoticeProps.path = record.openPage;
        dynamicNoticeProps.formData = { id: record.busId };
        instance.refs.dynamicNoticeRef?.detail(record.openPage);
      } else {
        detailModal.openModal(true, {
          record,
          isUpdate: true,
        });
      }
      popoverVisible.value = false;
    }

    // 初始化 WebSocket
    function initWebSocket() {
      let userId = unref(userStore.getUserInfo).id;
      let token = getToken();
      // WebSocket与普通的请求所用协议有所不同，ws等同于http，wss等同于https
      let url = glob.domainUrl?.replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId;
      connectWebSocket(url);
      onWebSocket(onWebSocketMessage);
    }

    function onWebSocketMessage(data) {
      console.log('---onWebSocketMessage---', data)
      if (data.cmd === 'topic' || data.cmd === 'user') {
        //update-begin-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知，为什么必须刷新右上角才提示
        //后台保存数据太慢 前端延迟刷新消息
        setTimeout(() => {
          loadData();
        }, 1000)
        //update-end-author:taoyan date:2022-7-13 for: VUEN-1674【严重bug】系统通知，为什么必须刷新右上角才提示
      }
    }

    // 清空消息
    async function onEmptyNotify() {
      popoverVisible.value = false;
      await updateAllUnreadMessage();
      loadData();
    }

    return {
      prefixCls,
      count,
      tabKey,
      tabDataList,
      tabTypeList,
      currentPage,
      msgCategory,
      tabChange,
      onNoticeClick,
      onEmptyNotify,
      numberStyle: {},
      popoverVisible,
      registerDetail,
      dynamicNoticeProps,
    };
  },
});
</script>
<style lang="less" scoped>
//noinspection LessUnresolvedVariable
@prefix-cls: ~'@{namespace}-header-notify';

.@{prefix-cls} {
  padding-top: 2px;

  &__overlay {
    max-width: 340px;

    .ant-popover-inner-content {
      padding: 0;
    }

    .ant-tabs-nav {
      margin-bottom: 12px;
    }

    .ant-list-item {
      padding: 12px 24px;
      transition: background-color 300ms;

      &:hover {
        background-color: #e6f7ff;
      }
    }

    .bottom-buttons {
      text-align: center;
      border-top: 1px solid #f0f0f0;
      height: 42px;

      .ant-btn {
        border: 0;
        height: 100%;

        &:first-child {
          border-right: 1px solid #f0f0f0;
        }
      }
    }
  }

  .ant-tabs-content {
    width: 300px;
  }

  .ant-badge {
    font-size: 18px;

    .ant-badge-count {
      @badget-size: 16px;
      width: @badget-size;
      height: @badget-size;
      min-width: @badget-size;
      line-height: @badget-size;
      padding: 0;

      .ant-scroll-number-only>p.ant-scroll-number-only-unit {
        font-size: 14px;
        height: @badget-size;
      }
    }

    .ant-badge-multiple-words {
      padding: 0 0 0 2px;
      font-size: 12px;
    }

    svg {
      width: 0.9em;
    }
  }
}

// 兼容黑暗模式
[data-theme='dark'] .@{prefix-cls} {
  &__overlay {
    .ant-list-item {
      &:hover {
        background-color: #111b26;
      }
    }

    .bottom-buttons {
      border-top: 1px solid #303030;

      .ant-btn {
        &:first-child {
          border-right: 1px solid #303030;
        }
      }
    }
  }
}
</style>
